import 'package:flutter_study/project/_01osc/constants/constants.dart'
    show AppColors;
import 'package:flutter_study/project/_01osc/pages/discovery_page.dart';
import 'package:flutter_study/project/_01osc/pages/news_list_page.dart';
import 'package:flutter_study/project/_01osc/pages/profile_page.dart';
import 'package:flutter_study/project/_01osc/pages/tweet_page.dart';
import 'package:flutter_study/project/_01osc/widgets/my_drawer.dart';
import 'package:flutter_study/project/_01osc/widgets/navigation_icon_view.dart';
import 'package:flutter/material.dart';

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  final _appBarTitle = ['资讯', '动弹', '发现', '我的'];
  var _currentIndex = 0;
  List<NavigationIconView> _navigationIconViews;
  List<Widget> _pages;
  PageController _pageController;

  @override
  void initState() {
    super.initState();
    _pages = [NewsListPage(), TweetPage(), DiscoveryPage(), ProfilePage()];
    _pageController = PageController(initialPage: _currentIndex);
    _navigationIconViews = [
      NavigationIconView(
          title: '资讯',
          iconPath: 'assets/images/ic_nav_news_normal.png',
          activeIconPath: 'assets/images/ic_nav_news_actived.png'),
      NavigationIconView(
          title: '动弹',
          iconPath: 'assets/images/ic_nav_tweet_normal.png',
          activeIconPath: 'assets/images/ic_nav_tweet_actived.png'),
      NavigationIconView(
          title: '发现',
          iconPath: 'assets/images/ic_nav_discover_normal.png',
          activeIconPath: 'assets/images/ic_nav_discover_actived.png'),
      NavigationIconView(
          title: '我的',
          iconPath: 'assets/images/ic_nav_my_normal.png',
          activeIconPath: 'assets/images/ic_nav_my_pressed.png'),
    ];
  }

  @override
  Widget build(BuildContext context) {
    // SafeArea 适配异形屏
    return SafeArea(
      child: Scaffold(
        appBar: AppBar(
          elevation: 0.0, // 去掉AppBar的阴影
          title: Text(
            _appBarTitle[_currentIndex],
            style: TextStyle(color: Color(AppColors.APPBAR)),
          ),
          iconTheme: IconThemeData(color: Color(AppColors.APPBAR)),
        ),
        body: PageView.builder(
          physics: NeverScrollableScrollPhysics(), // 禁止pageView左右滑动切换
          itemBuilder: (context, index) {
            return _pages[index];
          },
          controller: _pageController,
          onPageChanged: (index) {
            setState(() {
              _currentIndex = index;
            });
          },
        ),
        bottomNavigationBar: BottomNavigationBar(
          items: _navigationIconViews.map((e) => e.item).toList(),
          currentIndex: _currentIndex,
          type: BottomNavigationBarType.fixed,
          onTap: (index) {
            setState(() {
              _currentIndex = index;
              _pageController.animateToPage(index,
                  duration: Duration(microseconds: 1), curve: Curves.ease);
            });
          },
        ),
        drawer: MyDrawer(
          headImgPath: 'assets/images/cover_img.jpg',
          iconList: [Icons.send, Icons.home, Icons.error, Icons.settings],
          menuList: ['发布动弹', '动弹小黑屋', '关于', '设置'],
        ),
      ),
    );
  }
}
